package com.tc.controller;

import com.tc.dto.Reimbursement_recordDTO;
import com.tc.dto.Reimbursement_recordQueryDTO;
import com.tc.service.IReimbursement_recordService;
import com.tc.utils.ResultVo;
import com.tc.vo.PageResultVO;
import com.tc.vo.Reimbursement_recordVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * <p>
 * 报销记录前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/reimbursement_record")
@Api(tags = "报销记录管理")
public class Reimbursement_recordController {

    @Autowired
    private IReimbursement_recordService reimbursementRecordService;

    @ApiOperation(value = "分页查询报销记录")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Reimbursement_recordVO>> pageQuery(@RequestBody Reimbursement_recordQueryDTO queryDTO) {
        PageResultVO<Reimbursement_recordVO> pageResultVO = reimbursementRecordService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }

    @ApiOperation(value = "根据ID查询报销记录")
    @ApiImplicitParam(name = "id", value = "报销记录ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Reimbursement_recordVO> getById(@PathVariable Long id) {
        Reimbursement_recordVO vo = reimbursementRecordService.getReimbursementRecordById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("报销记录不存在");
        }
    }

    @ApiOperation(value = "新增报销记录")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Reimbursement_recordDTO recordDTO) {
        boolean success = reimbursementRecordService.addReimbursementRecord(recordDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改报销记录")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Reimbursement_recordDTO recordDTO) {
        if (recordDTO.getRecordId() == null) {
            return ResultVo.fail("ID不能为空");
        }
        
        boolean success = reimbursementRecordService.updateReimbursementRecord(recordDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除报销记录")
    @ApiImplicitParam(name = "id", value = "报销记录ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean success = reimbursementRecordService.deleteReimbursementRecord(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
    
    @ApiOperation(value = "计算报销金额")
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "Long", paramType = "query"),
//        @ApiImplicitParam(name = "totalCost", value = "总费用", required = true, dataType = "BigDecimal", paramType = "query")
//    })
//    @GetMapping("/calculate")
//    public ResultVo<BigDecimal> calculateReimbursement(
//            @RequestParam Long patientId,
//            @RequestParam BigDecimal totalCost) {
//        BigDecimal amount = reimbursementRecordService.calculateReimbursementAmount(patientId, totalCost);
//        return ResultVo.ok(amount, "计算成功");
//    }

    @GetMapping("/calculate")
    public ResultVo<Reimbursement_recordVO> calculateReimbursement(
            @RequestParam Long patientId,
            @RequestParam BigDecimal totalCost) {

        Reimbursement_recordVO vo = reimbursementRecordService.calculateReimbursementAmount(patientId, totalCost);

        if (vo.getReimbursementRatio() == null) {
            return ResultVo.fail("计算失败，报销比例不存在");
        }
        // 返回计算结果

        return ResultVo.ok(vo, "计算成功");

    }
}

